package com.swn.demo.addreess.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.swn.common.util.DbHelper;
import com.swn.common.util.UUIDUtil;
import com.swn.demo.address.service.WeiXinAddressService;

/**
 * 
* @ClassName: Demo4ServiceImpl
* @Description: TODO(这里用一句话描述这个类的作用)
* @author dongliang
* @date 2018年8月9日
*
 */
@Service("weiXinAddressService")
public class WeiXinAddressServiceImpl implements WeiXinAddressService{

	private static Logger logger = Logger.getLogger("WeiXinAddressService.class");
	
	@Autowired
	private DbHelper dbHelper;
	
	
	/**
	 * 
	* @Title: saveOrUpdateAddress
	* @Description: 保存或者修改收货地址
	* @author dongliang
	* @data 2018年8月9日    参数
	* @throws
	*/
	@Override
	public String saveOrUpdateAddress(Map<String, Object> map) {
		
		try {
			if(map.containsKey("isDefault")&&"1".equals(map.get("isDefault").toString())){
				String sql = "update weixin_address set isDefault=0 where customerId=:customerId and isDefault=1";
				//System.out.println("sql"+sql);
				dbHelper.update(sql, map);
			}
			if(!map.containsKey("id")){
				//System.out.println("不包含 ID");
				String id = UUIDUtil.getUUID();
				map.put("id", id);
				dbHelper.saveMap(map, "weixin_address");
				return id;
			}else{
				//System.out.println("!!!!!!!!!");
				dbHelper.updateMap(map, "weixin_address");
				return "";
			}
		} catch (Exception e) {
			System.out.println("异常信息   "+e.getMessage() );
			logger.error(e.getMessage(), e);
			throw new RuntimeException();
		}
	}
	


	/**
	 * 
	* @Title: queryAllAddress
	* @Description: TODO(这里用一句话描述这个方法的作用)
	* @author dongliang
	* @data 2018年8月9日    参数
	* @throws
	*/
	@Override
	public List<Map<String, Object>> queryAllAddress(String user_id) {
		StringBuilder sql = new StringBuilder();
		sql.append("select id,receiver,receiverPhone,receiverProvince,receiverCity,receiverArea,receiverAddress")
		   .append(",isDefault from weixin_address")
		   .append(" where customerId='").append(user_id).append("'");
		return dbHelper.queryForList(sql.toString(), null);
	}

	
	/**
	 * 
	* @Title: deleteAddress
	* @Description: TODO(这里用一句话描述这个方法的作用)
	* @author dongliang
	* @data 2018年8月9日    参数
	* @throws
	*/
	@Override
	public int deleteAddress(String id) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		return dbHelper.delete(map, "weixin_address");
	}

}
